package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BsMenpiaoCategory;
import com.ruoyi.system.domain.BsOrderDetails;
import com.ruoyi.system.domain.BsUserContract;
import com.ruoyi.system.domain.vo.UserContractVo;
import com.ruoyi.system.mapper.BsOrderDetailsMapper;
import com.ruoyi.system.service.BsMenpiaoCategoryService;
import com.ruoyi.system.service.BsMenpiaoNumService;
import com.ruoyi.system.service.BsOrderDetailsService;
import com.ruoyi.system.service.BsUserContractService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class BsOrderDetailsServiceImpl extends ServiceImpl<BsOrderDetailsMapper, BsOrderDetails>
    implements BsOrderDetailsService{

    @Resource
    private BsMenpiaoNumService bsMenpiaoNumService;
    @Resource
    private BsMenpiaoCategoryService bsMenpiaoCategoryService;
    @Resource
    private BsUserContractService bsUserContractService;

    @Override
    public boolean updateNumAndPrice(BsOrderDetails bsOrderDetails) {
        BsOrderDetails oldEntity = getById(bsOrderDetails.getOrderDetailsId());
        BsMenpiaoCategory menpiaoCategory = bsMenpiaoNumService.getCategoryByNumId(oldEntity.getMenpiaoNumId());
        BigDecimal tmpPrice = null;
        if (bsOrderDetails.getOrderNum() > oldEntity.getOrderNum()){
            tmpPrice = oldEntity.getOrderPrice().add(menpiaoCategory.getMenpiaoPrice().multiply(BigDecimal.valueOf(bsOrderDetails.getOrderNum() - oldEntity.getOrderNum())));
        } else if(bsOrderDetails.getOrderNum() < oldEntity.getOrderNum()){
            tmpPrice = oldEntity.getOrderPrice().subtract(menpiaoCategory.getMenpiaoPrice().multiply(BigDecimal.valueOf(bsOrderDetails.getOrderNum() - oldEntity.getOrderNum())));
        } else {
            return false;
        }
        oldEntity.setOrderNum(bsOrderDetails.getOrderNum());
        oldEntity.setOrderPrice(tmpPrice);
        return updateById(oldEntity);
    }

    @Override
    public boolean saveContract(List<UserContractVo> contracts) {
        Map<Long, List<UserContractVo>> orderDetailsGroup = contracts.stream().collect(Collectors.groupingBy(UserContractVo::getOrderDetailsId));
        Set<Long> orderDetailsIdSet = orderDetailsGroup.keySet();
        List<BsOrderDetails> updateList = new ArrayList<>();
        for (Long orderDetailsId : orderDetailsIdSet) {
            StringBuilder sb = new StringBuilder();
            List<UserContractVo> userContractVos = orderDetailsGroup.get(orderDetailsId);
            for (UserContractVo userContractVo : userContractVos) {
                if (StringUtils.isNotNull(userContractVo.getContractId())){
                    sb.append(userContractVo.getContractId());
                } else {
                    BsUserContract one = bsUserContractService.getOne(new LambdaQueryWrapper<BsUserContract>().eq(BsUserContract::getContractCard, userContractVo.getContractCard()).eq(BsUserContract::getUserId, SecurityUtils.getUserId()));
                    if (StringUtils.isNotNull(one)){
                        sb.append(one.getContractId());
                    } else {
                        BsUserContract bsUserContract = ConvertUtil.sourceToTarget(userContractVo, BsUserContract.class);
                        bsUserContract.setUserId(SecurityUtils.getUserId());
                        bsUserContractService.save(bsUserContract);
                        sb.append(bsUserContract.getContractId());
                    }
                }
                sb.append(",");
            }
            String contractIds = sb.toString();
            contractIds = contractIds.substring(0,contractIds.length() - 1);
            BsOrderDetails one = getById(orderDetailsId);
            one.setContractId(contractIds);
            updateList.add(one);
        }
        return updateBatchById(updateList);
    }
}




